home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / specfun.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  21KB  |  848 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: specfun.c%v 3.50.1.9 1993/08/05 05:38:59 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /** GNUPLOT - specfun.c
  7.  *
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley,
  9.  *                                              Jos van der Woude
  10.  *
  11.  * Permission to use, copy, and distribute this software and its
  12.  * documentation for any purpose with or without fee is hereby granted,
  13.  * provided that the above copyright notice appear in all copies and
  14.  * that both that copyright notice and this permission notice appear
  15.  * in supporting documentation.
  16.  *
  17.  * Permission to modify the software is granted, but not the right to
  18.  * distribute the modified code.  Modifications are to be distributed
  19.  * as patches to released version.
  20.  *
  21.  * This software is provided "as is" without express or implied warranty.
  22.  *
  23.  *
  24.  * AUTHORS
  25.  *
  26.  *   Original Software:
  27.  *   Jos van der Woude, jvdwoude@hut.nl
  28.  *
  29.  * There is a mailing list for gnuplot users. Note, however, that the
  30.  * newsgroup 
  31.  *    comp.graphics.gnuplot 
  32.  * is identical to the mailing list (they
  33.  * both carry the same set of messages). We prefer that you read the
  34.  * messages through that newsgroup, to subscribing to the mailing list.
  35.  * (If you can read that newsgroup, and are already on the mailing list,
  36.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  37.  * removed from the mailing list.)
  38.  *
  39.  * The address for mailing to list members is
  40.  *       info-gnuplot@dartmouth.edu
  41.  * and for mailing administrative requests is 
  42.  *       info-gnuplot-request@dartmouth.edu
  43.  * The mailing list for bug reports is 
  44.  *       bug-gnuplot@dartmouth.edu
  45.  * The list of those interested in beta-test versions is
  46.  *       info-gnuplot-beta@dartmouth.edu
  47.  */
  48.  
  49. #include <math.h>
  50. #include <stdio.h>
  51. #include "plot.h"
  52.  
  53. #ifdef vms
  54. #include <errno.h>
  55. #else
  56. extern int errno;
  57. #endif /* vms */
  58.  
  59.  
  60. extern struct value stack[STACK_DEPTH];
  61. extern int s_p;
  62. extern double zero;
  63.  
  64. struct value *pop(), *Gcomplex(), *Ginteger();
  65.  
  66. double magnitude(), angle(), real(), imag();
  67.  
  68. #define ITMAX   100
  69. #ifdef FLT_EPSILON
  70. #define MACHEPS FLT_EPSILON /* 1.0E-08 */
  71. #else
  72. #define MACHEPS 1.0E-08
  73. #endif
  74. #ifdef FLT_MIN_EXP
  75. #define MINEXP  FLT_MIN_EXP /* -88.0 */
  76. #else
  77. #define MINEXP  -88.0
  78. #endif
  79. #ifdef FLT_MAX
  80. #define OFLOW   FLT_MAX /* 1.0E+37 */
  81. #else
  82. #define OFLOW   1.0E+37
  83. #endif
  84. #ifdef FLT_MAX_10_EXP
  85. #define XBIG    FLT_MAX_10_EXP /* 2.55E+305 */
  86. #else
  87. #define XBIG    2.55E+305
  88. #endif
  89.  
  90. /*
  91.  * Mathematical constants
  92.  */
  93. #define LNPI 1.14472988584940016
  94. #define LNSQRT2PI 0.9189385332046727
  95. #define PI 3.14159265358979323846
  96. #define PNT68 0.6796875
  97. #define SQRTPI 0.9189385332046727417803297
  98. #define SQRT_TWO 1.41421356237309504880168872420969809   /* JG */
  99.  
  100. #ifndef min /* GCC ST uses inline functions */
  101. #define min(a,b) ((a) < (b) ? (a) : (b))
  102. #endif
  103.  
  104. /* Global variables, not visible outside this file */
  105. #ifndef GAMMA
  106. static int signgam = 0;
  107. #else
  108. extern int signgam;
  109. #endif
  110. static long     Xm1 = 2147483563L;
  111. static long     Xm2 = 2147483399L;
  112. static long     Xa1 = 40014L;
  113. static long     Xa2 = 40692L;
  114.  
  115. /* Local function declarations, not visible outside this file */
  116. static double confrac();
  117. static double ibeta();
  118. static double igamma();
  119. static double ranf();
  120.  
  121. #ifndef GAMMA
  122. /* Provide GAMMA function for those who do not already have one */
  123. static double lngamma();
  124. static double lgamneg();
  125. static double lgampos();
  126.  
  127. /**
  128.  * from statlib, Thu Jan 23 15:02:27 EST 1992 ***
  129.  *
  130.  * This file contains two algorithms for the logarithm of the gamma function.
  131.  * Algorithm AS 245 is the faster (but longer) and gives an accuracy of about
  132.  * 10-12 significant decimal digits except for small regions around X = 1 and
  133.  * X = 2, where the function goes to zero.
  134.  * The second algorithm is not part of the AS algorithms.   It is slower but
  135.  * gives 14 or more significant decimal digits accuracy, except around X = 1
  136.  * and X = 2.   The Lanczos series from which this algorithm is derived is
  137.  * interesting in that it is a convergent series approximation for the gamma
  138.  * function, whereas the familiar series due to De Moivre (and usually wrongly
  139.  * called Stirling's approximation) is only an asymptotic approximation, as
  140.  * is the true and preferable approximation due to Stirling.
  141.  * 
  142.  * Uses Lanczos-type approximation to ln(gamma) for z > 0. Reference: Lanczos,
  143.  * C. 'A precision approximation of the gamma function', J. SIAM Numer.
  144.  * Anal., B, 1, 86-96, 1964. Accuracy: About 14 significant digits except for
  145.  * small regions in the vicinity of 1 and 2.
  146.  * 
  147.  * Programmer: Alan Miller CSIRO Division of Mathematics & Statistics
  148.  * 
  149.  * Latest revision - 17 April 1988
  150.  * 
  151.  * Additions: Translated from fortran to C, code added to handle values z < 0.
  152.  * The global variable signgam contains the sign of the gamma function.
  153.  * 
  154.  * IMPORTANT: The signgam variable contains garbage until AFTER the call to
  155.  * lngamma().
  156.  * 
  157.  * Permission granted to distribute freely for non-commercial purposes only
  158.  * Copyright (c) 1992 Jos van der Woude, jvdwoude@hut.nl
  159.  */
  160.  
  161. /* Local data, not visible outside this file 
  162. static double   a[] =
  163. {
  164.     0.9999999999995183E+00,
  165.     0.6765203681218835E+03,
  166.     -.1259139216722289E+04,
  167.     0.7713234287757674E+03,
  168.     -.1766150291498386E+03,
  169.     0.1250734324009056E+02,
  170.     -.1385710331296526E+00,
  171.     0.9934937113930748E-05,
  172.     0.1659470187408462E-06,
  173. };   */
  174.  
  175. /* from Ray Toy */
  176. static double a[] = {
  177.         .99999999999980993227684700473478296744476168282198,
  178.      676.52036812188509856700919044401903816411251975244084,
  179.    -1259.13921672240287047156078755282840836424300664868028,
  180.      771.32342877765307884865282588943070775227268469602500,
  181.     -176.61502916214059906584551353999392943274507608117860,
  182.       12.50734327868690481445893685327104972970563021816420,
  183.        -.13857109526572011689554706984971501358032683492780,
  184.         .00000998436957801957085956266828104544089848531228,
  185.         .00000015056327351493115583383579667028994545044040,
  186. };
  187.  
  188. static double   lgamneg(z)
  189. double z;
  190. {
  191.     double          tmp;
  192.  
  193.     /* Use reflection formula, then call lgampos() */
  194.     tmp = sin(z * PI);
  195.  
  196.     if (fabs(tmp) < MACHEPS) {
  197.     tmp = 0.0;
  198.     } else if (tmp < 0.0) {
  199.     tmp = -tmp;
  200.         signgam = -1;
  201.     }
  202.     return LNPI - lgampos(1.0 - z) - log(tmp);
  203.  
  204. }
  205.  
  206. static double   lgampos(z)
  207. double z;
  208. {
  209.     double          sum;
  210.     double          tmp;
  211.     int             i;
  212.  
  213.     sum = a[0];
  214.     for (i = 1, tmp = z; i < 9; i++) {
  215.         sum += a[i] / tmp;
  216.     tmp++;
  217.     }
  218.  
  219.     return log(sum) + LNSQRT2PI - z - 6.5 + (z - 0.5) * log(z + 6.5);
  220. }
  221.  
  222. static double lngamma(z)
  223. double z;
  224. {
  225.     signgam = 1.0;
  226.  
  227.     if (z <= 0.0)
  228.     return lgamneg(z);
  229.     else
  230.     return lgampos(z);
  231. }
  232.  
  233. #define GAMMA lngamma
  234. #endif /* GAMMA */
  235.  
  236. f_erf()
  237. {
  238. struct value a;
  239. double x;
  240.  
  241.         x = real(pop(&a));
  242. #ifndef ERF
  243.         x = x < 0.0 ? -igamma(0.5, x * x) : igamma(0.5, x * x);
  244. #else
  245.         x = erf(x);
  246. #endif
  247.         push( Gcomplex(&a,x,0.0) );
  248. }
  249.  
  250. f_erfc()
  251. {
  252. struct value a;
  253. double x;
  254.  
  255.         x = real(pop(&a));
  256. #ifndef ERF
  257.         x = x < 0.0 ? 1.0 + igamma(0.5, x * x) : 1.0 - igamma(0.5, x * x);
  258. #else
  259.         x = erfc(x);
  260. #endif
  261.         push( Gcomplex(&a,x,0.0) );
  262. }
  263.  
  264. f_ibeta()
  265. {
  266. struct value a;
  267. double x;
  268. double arg1;
  269. double arg2;
  270.  
  271.     x = real(pop(&a));
  272.     arg2 = real(pop(&a));
  273.     arg1 = real(pop(&a));
  274.  
  275.     x = ibeta(arg1, arg2, x);
  276.     if(x == -1.0) {
  277.         undefined = TRUE;
  278.         push( Ginteger(&a,0) );
  279.     } else
  280.         push( Gcomplex(&a,x,0.0) );
  281. }
  282.  
  283. f_igamma()
  284. {
  285. struct value a;
  286. double x;
  287. double arg1;
  288.  
  289.     x = real(pop(&a));
  290.     arg1 = real(pop(&a));
  291.  
  292.     x = igamma(arg1,x);
  293.     if(x == -1.0) {
  294.         undefined = TRUE;
  295.         push( Ginteger(&a,0) );
  296.     } else
  297.         push( Gcomplex(&a,x,0.0) );
  298. }
  299.  
  300. f_gamma()
  301. {
  302. register double y;
  303. struct value a;
  304.  
  305.         y = GAMMA(real(pop(&a)));
  306.     if (y > 88.0) {
  307.         undefined = TRUE;
  308.         push( Ginteger(&a,0) );
  309.     }
  310.     else
  311.         push( Gcomplex(&a,signgam * exp(y),0.0) );
  312. }
  313.  
  314. f_lgamma()
  315. {
  316. struct value a;
  317.  
  318.         push( Gcomplex(&a, GAMMA(real(pop(&a))),0.0) );
  319. }
  320.  
  321. #ifndef BADRAND
  322.  
  323. f_rand()
  324. {
  325. struct value a;
  326.  
  327.         push( Gcomplex(&a, ranf(real(pop(&a))),0.0) );
  328. }
  329.  
  330. #else
  331.  
  332. /* Use only to observe the effect of a "bad" random number generator. */
  333. f_rand()
  334. {
  335. struct value a;
  336.  
  337. static unsigned int y =0;
  338. unsigned int maxran = 1000;
  339.  
  340.     (void)real(pop(&a));
  341.     y = (781*y + 387) %maxran;
  342.  
  343.     push( Gcomplex(&a, (double) y /maxran,0.0) );
  344. }
  345.  
  346. #endif
  347.  
  348. /** ibeta.c
  349.  *
  350.  *   DESCRIB   Approximate the incomplete beta function Ix(a, b).
  351.  *
  352.  *                           _
  353.  *                          |(a + b)     /x  (a-1)         (b-1)
  354.  *             Ix(a, b) = -_-------_--- * |  t     * (1 - t)     dt (a,b > 0)
  355.  *                        |(a) * |(b)   /0
  356.  *
  357.  *
  358.  *
  359.  *   CALL      p = ibeta(a, b, x)
  360.  *
  361.  *             double    a    > 0
  362.  *             double    b    > 0
  363.  *             double    x    [0, 1]
  364.  *
  365.  *   WARNING   none
  366.  *
  367.  *   RETURN    double    p    [0, 1]
  368.  *                            -1.0 on error condition
  369.  *
  370.  *   XREF      lngamma()
  371.  *
  372.  *   BUGS      none
  373.  *
  374.  *   REFERENCE The continued fraction expansion as given by
  375.  *             Abramowitz and Stegun (1964) is used.
  376.  *
  377.  * Permission granted to distribute freely for non-commercial purposes only
  378.  * Copyright (c) 1992 Jos van der Woude, jvdwoude@hut.nl
  379.  */
  380.  
  381. static double          ibeta(a, b, x)
  382. double a, b, x;
  383. {
  384.     /* Test for admissibility of arguments */
  385.     if (a <= 0.0 || b <= 0.0)
  386.     return -1.0;
  387.     if (x < 0.0 || x > 1.0)
  388.     return -1.0;;
  389.  
  390.     /* If x equals 0 or 1, return x as prob */
  391.     if (x == 0.0 || x == 1.0)
  392.     return x;
  393.  
  394.     /* Swap a, b if necessarry for more efficient evaluation */
  395.     return a < x * (a + b) ? 1.0 - confrac(b, a, 1.0 - x) : confrac(a, b, x);
  396. }
  397.  
  398. static double   confrac(a, b, x)
  399. double a, b, x;
  400. {
  401.     double          Alo = 0.0;
  402.     double          Ahi;
  403.     double          Aev;
  404.     double          Aod;
  405.     double          Blo = 1.0;
  406.     double          Bhi = 1.0;
  407.     double          Bod = 1.0;
  408.     double          Bev = 1.0;
  409.     double          f;
  410.     double          fold;
  411.     double          Apb = a + b;
  412.     double          d;
  413.     int             i;
  414.     int             j;
  415.  
  416.     /* Set up continued fraction expansion evaluation. */
  417.     Ahi = exp(GAMMA(Apb) + a * log(x) + b * log(1.0 - x) -
  418.               GAMMA(a + 1.0) - GAMMA(b));
  419.  
  420.     /*
  421.      * Continued fraction loop begins here. Evaluation continues until
  422.      * maximum iterations are exceeded, or convergence achieved.
  423.      */
  424.     for (i = 0, j = 1, f = Ahi; i <= ITMAX; i++, j++) {
  425.     d = a + j + i;
  426.     Aev = -(a + i) * (Apb + i) * x / d / (d - 1.0);
  427.     Aod = j * (b - j) * x / d / (d + 1.0);
  428.     Alo = Bev * Ahi + Aev * Alo;
  429.     Blo = Bev * Bhi + Aev * Blo;
  430.     Ahi = Bod * Alo + Aod * Ahi;
  431.     Bhi = Bod * Blo + Aod * Bhi;
  432.  
  433.     if (fabs(Bhi) < MACHEPS)
  434.         Bhi = 0.0;
  435.  
  436.     if (Bhi != 0.0) {
  437.         fold = f;
  438.         f = Ahi / Bhi;
  439.         if (fabs(f - fold) < fabs(f) * MACHEPS)
  440.         return f;
  441.     }
  442.     }
  443.  
  444.     return -1.0;
  445. }
  446.  
  447. /** igamma.c
  448.  *
  449.  *   DESCRIB   Approximate the incomplete gamma function P(a, x).
  450.  *
  451.  *                         1     /x  -t   (a-1)
  452.  *             P(a, x) = -_--- * |  e  * t     dt      (a > 0)
  453.  *                       |(a)   /0
  454.  *
  455.  *   CALL      p = igamma(a, x)
  456.  *
  457.  *             double    a    >  0
  458.  *             double    x    >= 0
  459.  *
  460.  *   WARNING   none
  461.  *
  462.  *   RETURN    double    p    [0, 1]
  463.  *                            -1.0 on error condition
  464.  *
  465.  *   XREF      lngamma()
  466.  *
  467.  *   BUGS      Values 0 <= x <= 1 may lead to inaccurate results.
  468.  *
  469.  *   REFERENCE ALGORITHM AS239  APPL. STATIST. (1988) VOL. 37, NO. 3
  470.  *
  471.  * Permission granted to distribute freely for non-commercial purposes only
  472.  * Copyright (c) 1992 Jos van der Woude, jvdwoude@hut.nl
  473.  */
  474.  
  475. /* Global variables, not visible outside this file */
  476. static double   pn1, pn2, pn3, pn4, pn5, pn6;
  477.  
  478. static double          igamma(a, x)
  479. double a, x;
  480. {
  481.     double          arg;
  482.     double          aa;
  483.     double          an;
  484.     double          b;
  485.     int             i;
  486.  
  487.     /* Check that we have valid values for a and x */
  488.     if (x < 0.0 || a <= 0.0)
  489.     return -1.0;
  490.  
  491.     /* Deal with special cases */
  492.     if (x == 0.0)
  493.     return 0.0;
  494.     if (x > XBIG)
  495.     return 1.0;
  496.  
  497.     /* Check value of factor arg */
  498.     arg = a * log(x) - x - GAMMA(a + 1.0);
  499.     if (arg < MINEXP)
  500.     return -1.0;
  501.     arg = exp(arg);
  502.  
  503.     /* Choose infinite series or continued fraction. */
  504.  
  505.     if ((x > 1.0) && (x >= a + 2.0)) {
  506.     /* Use a continued fraction expansion */
  507.  
  508.     double          rn;
  509.     double          rnold;
  510.  
  511.     aa = 1.0 - a;
  512.     b = aa + x + 1.0;
  513.     pn1 = 1.0;
  514.     pn2 = x;
  515.     pn3 = x + 1.0;
  516.     pn4 = x * b;
  517.     rnold = pn3 / pn4;
  518.  
  519.     for (i = 1; i <= ITMAX; i++) {
  520.  
  521.         aa++;
  522.         b += 2.0;
  523.         an = aa * (double) i;
  524.  
  525.         pn5 = b * pn3 - an * pn1;
  526.         pn6 = b * pn4 - an * pn2;
  527.  
  528.         if (pn6 != 0.0) {
  529.  
  530.         rn = pn5 / pn6;
  531.         if (fabs(rnold - rn) <= min(MACHEPS, MACHEPS * rn))
  532.             return 1.0 - arg * rn * a;
  533.  
  534.         rnold = rn;
  535.         }
  536.         pn1 = pn3;
  537.         pn2 = pn4;
  538.         pn3 = pn5;
  539.         pn4 = pn6;
  540.  
  541.         /* Re-scale terms in continued fraction if terms are large */
  542.         if (fabs(pn5) >= OFLOW) {
  543.  
  544.         pn1 /= OFLOW;
  545.         pn2 /= OFLOW;
  546.         pn3 /= OFLOW;
  547.         pn4 /= OFLOW;
  548.         }
  549.     }
  550.     } else {
  551.     /* Use Pearson's series expansion. */
  552.  
  553.     for (i = 0, aa = a, an = b = 1.0; i <= ITMAX; i++) {
  554.  
  555.         aa++;
  556.         an *= x / aa;
  557.         b += an;
  558.         if (an < b * MACHEPS)
  559.         return arg * b;
  560.     }
  561.     }
  562.     return -1.0;
  563. }
  564.  
  565. /***********************************************************************
  566.      double ranf(double init)
  567.                 RANDom number generator as a Function
  568.      Returns a random floating point number from a uniform distribution
  569.      over 0 - 1 (endpoints of this interval are not returned) using a
  570.      large integer generator.
  571.      This is a transcription from Pascal to Fortran of routine
  572.      Uniform_01 from the paper
  573.      L'Ecuyer, P. and Cote, S. "Implementing a Random Number Package
  574.      with Splitting Facilities." ACM Transactions on Mathematical
  575.      Software, 17:98-111 (1991)
  576.  
  577.                GeNerate LarGe Integer
  578.      Returns a random integer following a uniform distribution over
  579.      (1, 2147483562) using the generator.
  580.      This is a transcription from Pascal to Fortran of routine
  581.      Random from the paper
  582.      L'Ecuyer, P. and Cote, S. "Implementing a Random Number Package
  583.      with Splitting Facilities." ACM Transactions on Mathematical
  584.      Software, 17:98-111 (1991)
  585. ***********************************************************************/
  586. static double          ranf(init)
  587. double init;
  588. {
  589.  
  590.     long            k, z;
  591.     static int      firsttime = 1;
  592.     static long     s1, s2;
  593.  
  594.     /* (Re)-Initialize seeds if necessary */
  595.     if (init < 0.0 || firsttime == 1) {
  596.     firsttime = 0;
  597.     s1 = 1234567890L;
  598.     s2 = 1234567890L;
  599.     }
  600.     /* Generate pseudo random integers */
  601.     k = s1 / 53668L;
  602.     s1 = Xa1 * (s1 - k * 53668L) - k * 12211;
  603.     if (s1 < 0)
  604.     s1 += Xm1;
  605.     k = s2 / 52774L;
  606.     s2 = Xa2 * (s2 - k * 52774L) - k * 3791;
  607.     if (s2 < 0)
  608.     s2 += Xm2;
  609.     z = s1 - s2;
  610.     if (z < 1)
  611.     z += (Xm1 - 1);
  612.  
  613.     /*
  614.      * 4.656613057E-10 is 1/Xm1.  Xm1 is set at the top of this file and is
  615.      * currently 2147483563. If Xm1 changes, change this also.
  616.      */
  617.     return (double) 4.656613057E-10 *z;
  618. }
  619.  
  620. /* ----------------------------------------------------------------
  621.    Following to specfun.c made by John Grosh (jgrosh@arl.mil)
  622.    on 28 OCT 1992.
  623.    ---------------------------------------------------------------- */
  624.  
  625. f_normal()    /* Normal or Gaussian Probability Function */
  626. {
  627. struct value a;
  628. double x;
  629.  
  630.     /* ref. Abramowitz and Stegun 1964, "Handbook of Mathematical 
  631.        Functions", Applied Mathematics Series, vol 55,
  632.        Chapter 26, page 934, Eqn. 26.2.29 and Jos van der Woude 
  633.            code found above */
  634.  
  635.     x = real(pop(&a));
  636.  
  637. #ifndef ERF
  638.         x = 0.5 * SQRT_TWO * x;
  639.         x = 0.5 * (1.0 + (x < 0.0 ? -igamma(0.5, x * x) : igamma(0.5, x * x)));
  640. #else
  641.     x = 0.5 * (1.0 + erf(0.5 * SQRT_TWO * x));
  642. #endif
  643.         push( Gcomplex(&a,x,0.0) );
  644. }
  645.  
  646. f_inverse_normal()  /* Inverse normal distribution function */
  647. {
  648. struct value a;
  649. double x;
  650. double inverse_normal_func();
  651.  
  652.     x = real(pop(&a));
  653.  
  654.     if (fabs(x) >= 1.0) {
  655.         undefined = TRUE;
  656.         push(Gcomplex(&a,0.0, 0.0));
  657.     } else {
  658.         push( Gcomplex(&a,inverse_normal_func(x), 0.0) );
  659.     }
  660. }
  661.  
  662.  
  663. f_inverse_erf()  /* Inverse error function */
  664. {
  665. struct value a;
  666. double x;
  667. double inverse_error_func();
  668.  
  669.     x = real(pop(&a));
  670.  
  671.     if (fabs(x) >= 1.0) {
  672.         undefined = TRUE;
  673.         push(Gcomplex(&a,0.0, 0.0));
  674.     } else {
  675.         push( Gcomplex(&a,inverse_error_func(x), 0.0) );
  676.     }
  677. }
  678.  
  679. double 
  680. inverse_normal_func(p)
  681. double p;
  682. {
  683.     /* 
  684.            Source: This routine was derived (using f2c) from the 
  685.                    FORTRAN subroutine MDNRIS found in 
  686.                    ACM Algorithm 602 obtained from netlib.
  687.  
  688.                    MDNRIS code contains the 1978 Copyright 
  689.                    by IMSL, INC. .  Since MDNRIS has been 
  690.                    submitted to netlib it may be used with 
  691.                    the restriction that it may only be 
  692.                    used for noncommercial purposes and that
  693.                    IMSL be acknowledged as the copyright-holder
  694.                    of the code.
  695.         */
  696.  
  697.     /* Initialized data */
  698.     static double eps = 1e-10;
  699.     static double g0 = 1.851159e-4;
  700.     static double g1 = -.002028152;
  701.     static double g2 = -.1498384;
  702.     static double g3 = .01078639;
  703.     static double h0 = .09952975;
  704.     static double h1 = .5211733;
  705.     static double h2 = -.06888301;
  706.     static double sqrt2 = 1.414213562373095;
  707.  
  708.     /* Local variables */
  709.     static double a, w, x;
  710.     static double sd, wi, sn, y;
  711.  
  712.     double inverse_error_func();
  713.  
  714.     /* Note: 0.0 < p < 1.0 */
  715.  
  716.     /* p too small, compute y directly */
  717.     if (p <= eps) {
  718.         a = p + p;
  719.         w = sqrt(-(double)log(a + (a - a * a)));
  720.  
  721.         /* use a rational function in 1.0 / w */
  722.         wi = 1.0 / w;
  723.         sn = ((g3 * wi + g2) * wi + g1) * wi;
  724.         sd = ((wi + h2) * wi + h1) * wi + h0;
  725.         y = w + w * (g0 + sn / sd);
  726.         y = -y * sqrt2;
  727.     } else {
  728.         x = 1.0 - (p + p);
  729.         y = inverse_error_func(x);
  730.         y = -sqrt2 * y;
  731.     }
  732.     return(y);
  733.  
  734.  
  735. double 
  736. inverse_error_func(p) 
  737. double p;
  738. {
  739.     /* 
  740.            Source: This routine was derived (using f2c) from the 
  741.                    FORTRAN subroutine MERFI found in 
  742.                    ACM Algorithm 602 obtained from netlib.
  743.  
  744.                    MDNRIS code contains the 1978 Copyright 
  745.                    by IMSL, INC. .  Since MERFI has been 
  746.                    submitted to netlib, it may be used with 
  747.                    the restriction that it may only be 
  748.                    used for noncommercial purposes and that
  749.                    IMSL be acknowledged as the copyright-holder
  750.                    of the code.
  751.         */
  752.  
  753.  
  754.  
  755.     /* Initialized data */
  756.     static double a1 = -.5751703;
  757.     static double a2 = -1.896513;
  758.     static double a3 = -.05496261;
  759.     static double b0 = -.113773;
  760.     static double b1 = -3.293474;
  761.     static double b2 = -2.374996;
  762.     static double b3 = -1.187515;
  763.     static double c0 = -.1146666;
  764.     static double c1 = -.1314774;
  765.     static double c2 = -.2368201;
  766.     static double c3 = .05073975;
  767.     static double d0 = -44.27977;
  768.     static double d1 = 21.98546;
  769.     static double d2 = -7.586103;
  770.     static double e0 = -.05668422;
  771.     static double e1 = .3937021;
  772.     static double e2 = -.3166501;
  773.     static double e3 = .06208963;
  774.     static double f0 = -6.266786;
  775.     static double f1 = 4.666263;
  776.     static double f2 = -2.962883;
  777.     static double g0 = 1.851159e-4;
  778.     static double g1 = -.002028152;
  779.     static double g2 = -.1498384;
  780.     static double g3 = .01078639;
  781.     static double h0 = .09952975;
  782.     static double h1 = .5211733;
  783.     static double h2 = -.06888301;
  784.  
  785.     /* Local variables */
  786.     static double a, b, f, w, x, y, z, sigma, z2, sd, wi, sn;
  787.  
  788.     x = p;
  789.  
  790.     /* determine sign of x */
  791.     if (x > 0)
  792.         sigma = 1.0;
  793.     else
  794.         sigma = -1.0;
  795.  
  796.     /* Note: -1.0 < x < 1.0 */
  797.  
  798.     z = fabs(x);
  799.  
  800.     /* z between 0.0 and 0.85, approx. f by a 
  801.        rational function in z  */
  802.  
  803.     if (z <= 0.85) {
  804.         z2 = z * z;
  805.         f = z + z * (b0 + a1 * z2 / (b1 + z2 + a2 
  806.             / (b2 + z2 + a3 / (b3 + z2))));
  807.  
  808.     /* z greater than 0.85 */
  809.     } else {
  810.         a = 1.0 - z;
  811.         b = z;
  812.  
  813.         /* reduced argument is in (0.85,1.0), 
  814.            obtain the transformed variable */
  815.  
  816.         w = sqrt(-(double)log(a + a * b));
  817.  
  818.         /* w greater than 4.0, approx. f by a 
  819.            rational function in 1.0 / w */
  820.  
  821.         if (w >= 4.0) {
  822.             wi = 1.0 / w;
  823.             sn = ((g3 * wi + g2) * wi + g1) * wi;
  824.             sd = ((wi + h2) * wi + h1) * wi + h0;
  825.             f = w + w * (g0 + sn / sd);
  826.  
  827.         /* w between 2.5 and 4.0, approx. 
  828.            f by a rational function in w */
  829.  
  830.         } else if (w < 4.0 && w > 2.5) {
  831.             sn = ((e3 * w + e2) * w + e1) * w;
  832.             sd = ((w + f2) * w + f1) * w + f0;
  833.             f = w + w * (e0 + sn / sd);
  834.  
  835.         /* w between 1.13222 and 2.5, approx. f by 
  836.            a rational function in w */
  837.         } else if (w <= 2.5 && w > 1.13222) {
  838.             sn = ((c3 * w + c2) * w + c1) * w;
  839.             sd = ((w + d2) * w + d1) * w + d0;
  840.             f = w + w * (c0 + sn / sd);
  841.         }
  842.     }
  843.     y = sigma * f;
  844.     return(y);
  845. }
  846.  
  847.